/*
 * 文件名：ProtocalSdtpDataEncode.java
 * 版权：亚信联创版权所有
 * 描述：TODO
 * 修改人：xuwei3
 * 修改时间：2015-5-20
 * 修改内容：TODO
 */
package com.ailk.bigdata.etl.realstream.server.protocal.sdtp.netty.handler;

import com.ailk.bigdata.etl.realstream.server.model.Header;
import com.ailk.bigdata.etl.realstream.server.model.SdtpDataPack;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

/**
 * @description TODO
 * @author [xuwei3]
 * @version [版本号,2015-5-20]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class ProtocalSdtpDataEncode extends MessageToByteEncoder<SdtpDataPack>
{

	/**
	 * @see io.netty.handler.codec.MessageToByteEncoder#encode(io.netty.channel.ChannelHandlerContext,
	 *      java.lang.Object, io.netty.buffer.ByteBuf)
	 */
	@Override
	protected void encode(ChannelHandlerContext channel, SdtpDataPack dataPack, ByteBuf byteBuf) throws Exception
	{
		Header header = dataPack.getHeader();
		
		byteBuf.writeShort(header.getTotalLength());
		byteBuf.writeShort(header.getMessageType());
		byteBuf.writeInt(header.getSequenceId().intValue());
		byteBuf.writeByte(header.getTotalContents());

		byteBuf.writeBytes(dataPack.getDataContent());

		header = null;
		dataPack = null;

	}

}
